/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.common.parsers.html;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * A wrapper for <code>InputStream</code> that:
 * <ul>
 * <li>
 * remembers all bytes read from the <b>stream</b> until
 * <code>stopRemembering()</code> is called
 * </li>
 * <li>
 * doesn't close the <b>stream</b> when <code>close()</code> is called
 * </li>
 * </ul>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 * &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2005/08/02 14:53:29 $
 */
class RewindableInputStreamWrapper extends InputStream {
    private InputStream stream;
    private ByteArrayOutputStream readBytes;
    private boolean remembering;

    private RewindableInputStreamWrapper() {}

    RewindableInputStreamWrapper(InputStream stream) {
        this.stream = stream;
        readBytes = new ByteArrayOutputStream();
        remembering = true;
    }

    public int available() throws IOException {
        return stream.available();
    }

    public void close() throws IOException {}

    public synchronized void reset() throws IOException {
        stream.reset();
    }

    public boolean markSupported() {
        return stream.markSupported();
    }

    public synchronized void mark(int readlimit) {
        stream.mark(readlimit);
    }

    public long skip(long n) throws IOException {
        return stream.skip(n);
    }

    public int read(byte[] b) throws IOException {
        return super.read(b);
    }

    public int read(byte[] b, int off, int len) throws IOException {
        return super.read(b, off, len);
    }

    public int read() throws IOException {
        int res = stream.read();
        if (remembering) {
            readBytes.write(res);
        }
        return res;
    }

    /**
     * Gets the <code>stream</code>.
     * @return the <code>stream</code> that is inside this wrapper
     */
    public InputStream getStream() {
        return stream;
    }

    /**
     * Returns bytes that were read from this stream, if
     * <code>stopRemembering</code> was not called, otherwise <code>null</code>.
     *
     * @return array of read bytes
     */
    public byte[] getReadBytes() {
        return (readBytes == null) ? null : readBytes.toByteArray();
    }

    /**
     * Stops saving read bytes.
     */
    public void stopRemembering() {
        remembering = false;
        readBytes = null;
    }
}
